/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package pingce.share.model.raw;

import java.util.ArrayList;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
//import org.w3c.dom.NamedNodeMap;
import org.w3c.dom.Node;
//import org.w3c.dom.NodeList;

import pingce.share.util.XMLProcessor;
import static pingce.share.util.Print.*;

/**
 *
 * @author ucai
 */
public class TestDocument {
    public ArrayList<Weibo> weibos;

    public TestDocument(){
        weibos = new ArrayList<Weibo>();
    }

    public static TestDocument getInstance(String xmlPath){
        TestDocument td = new TestDocument();
        try{
            XMLProcessor p = XMLProcessor.getInstance();
            Document d = p.getXmlDocument(xmlPath);
            
            Node tdNode = d.getFirstChild();

            if(tdNode != null){
                Node weiboNode = tdNode.getFirstChild();
                while(weiboNode != null){
                    if(weiboNode.getNodeName().equals("weibo"))
                    {
                        Weibo w = Weibo.getInstance(weiboNode);
                        td.weibos.add(w);
                    }
                  weiboNode = weiboNode.getNextSibling();
                }
            }
        }catch(Exception e){
            e.printStackTrace();
        }
        return td;
    }
/**
 *
 * @param xmlPath
 * @param rootElementName Name of the root element, such as "topic"/"result"
 */
    public void writeToFile(String xmlPath, String rootElementName){
         XMLProcessor p = XMLProcessor.getInstance();
         try{
            Document d = p.createXmlDocument();
            Element tdElement = d.createElement(rootElementName);

            for(Weibo w : this.weibos){
                Element weiboElement = w.writeToElement(d);
                tdElement.appendChild(weiboElement);
            }
            d.appendChild(tdElement);
            p.writeToFile(d, xmlPath);
         }catch(Exception e){
            e.printStackTrace();
         }
    }    

    public static void main(String[] args){
        String path = "C:/Users/ZZY/Desktop/评测/微博情感-样例数据/labelled test dataset/" +
        		"guan_yuan_cai_chan_gong_shi.xml.result.xml";
        TestDocument td = TestDocument.getInstance(path);
//        for(int i = 0; i < 10; i++){
//            System.out.println(td.weibos.get(i).sentences.size());
//            for(Sentence s : td.weibos.get(i).sentences){
//                System.out.println(s.targets.size());
//                if(s.targets.size() > 0){
//                    System.out.println(s.targets.get(0).text);
//                }
//            }
//        }
//        for(Weibo w : td.weibos){
//            if(w.forwards.size() > 0){
//                System.out.println(w.forwards.get(0).text);
//            }
//            if(w.id == 1556){
//            	for(Sentence s : w.sentences)
//            		System.out.println(s.text);
//            }
//        }
//        System.out.println(td.weibos.size());
        td.writeToFile(path + ".result", "topic");
    }
}
